// 权限判断公共方法
// result: boolean 按钮权限状态，item: 返回查询结果
export const getBtnPermission = function(btnCode, pagePermissions={}, result='boolean') {
    if (!pagePermissions || pagePermissions.length === 0) return false
    const { buttonList =[]} = pagePermissions
    const codes = btnCode.split(',') || []
    return result === 'item' ?
        buttonList.find(btnItem => codes.includes(btnItem.buttonCode))
        :
        buttonList.some(btnItem => codes.includes(btnItem.buttonCode))
}

export const getMenuPermission = function(menuCode, pagePermissions={}, judge='and') {
    if (!pagePermissions || pagePermissions.length === 0) return false
    const { childList =[]} = pagePermissions
    const codes = menuCode.split(',') || []
    return childList.some(child => codes.includes(child.menuCode))
}

// 按权限code匹配是否具有权限，code的格式为 menuCode_subMenuCode_actionCode, 对全部appPermissions权限进行匹配
export const getPermissionByCode = function(code='', appPermissions=[]) {
    console.log('getPermissionByCode', code, appPermissions);
    if(!appPermissions || !appPermissions.length) return false
    const [menuCode, pageCode, buttonCode] = code.split('_')
    const matchMenu = appPermissions.find(item => item.menuCode === menuCode)

    if (matchMenu && pageCode) {
        const { childList =[]} = matchMenu
        const matchPage = childList.find(item => item.menuCode === pageCode)
        if (matchPage && buttonCode) {
            const { buttonList =[]} = matchPage
            return buttonList.find(btn => btn.buttonCode === buttonCode) != undefined
        }
        return matchPage != undefined
    }
    return matchMenu != undefined
}
// 按权限code匹配是否具有权限，code的格式为 menuCode_subMenuCode_actionCode, 对全部appPermissions权限进行匹配
export const getPermissionItemByCode = function(code='', appPermissions=[]) {
    if(!appPermissions || appPermissions.length === 0) return undefined
    const [menuCode, pageCode, buttonCode] = code.split('_')
    const matchMenu = appPermissions.find(item => item.menuCode === menuCode)

    if (matchMenu && pageCode) {
        const { childList =[]} = matchMenu
        const matchPage = childList.find(item => item.menuCode === pageCode)
        if (matchPage && buttonCode) {
            const { buttonList =[]} = matchPage
            return buttonList.find(btn => btn.buttonCode === buttonCode)
        }
        return matchPage
    }
    return matchMenu
}